package com.yanxinim.cn.store.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yanxinim.cn.R;
import com.yanxinim.cn.store.bean.ProductType;

import java.util.ArrayList;
import java.util.List;

public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.ViewHolder> {
    private Context context;
    private List<ProductType> dataList = new ArrayList<>();
    private ItemOnClickListener itemOnClickListener;
    private int selectedPosition = 0;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public TypeAdapter(Context context, List<ProductType> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void addAllData(List<ProductType> dataListUpdate) {
        if (dataListUpdate != null && dataListUpdate.size() > 0) {
            dataList.addAll(dataListUpdate);
            notifyItemRangeChanged(dataList.size() - dataListUpdate.size(), dataListUpdate.size());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.text, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        ProductType item = dataList.get(position);
        if (item.isSelected()) {
            viewHolder.text.setBackgroundResource(R.color.main_color);
            viewHolder.text.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            viewHolder.text.setBackgroundResource(R.color.white);
            viewHolder.text.setTextColor(context.getResources().getColor(R.color.text_color));
        }
        viewHolder.text.setText(item.getCategoryname());
        viewHolder.itemView.setOnClickListener(v -> itemOnClickListener.onItemClick(item, position));
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.tv_text);
        }
    }

    public void updateItem(int position) {
        if (position != selectedPosition) {
            ProductType newType = dataList.get(selectedPosition);
            newType.setSelected(false);
            notifyItemChanged(selectedPosition, newType);

            ProductType newTypeUpdate = dataList.get(position);
            newTypeUpdate.setSelected(true);
            notifyItemChanged(position, newTypeUpdate);
            selectedPosition = position;
        }

    }

    public interface ItemOnClickListener {
        void onItemClick(ProductType newType, int position);
    }
}
